#!/bin/sh

# Slackware build script of SpiderOak
# Written by Diogo Leal (diogo@diogoleal.com)

PRGNAM=spideroak
VERSION=${VERSION:-9909}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

case "$( uname -m )" in
  i?86) ARCH=i386 ;;
  arm*) ARCH=arm ;;
     *) ARCH=$( uname -m ) ;;
esac

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "x86_64" ]; then
  SRCARCH=amd64
  LIBDIRSUFFIX="64"
elif [ "$ARCH" = "i386" ]; then
  SRCARCH=i386
  LIBDIRSUFFIX=""
else
  printf "\n\n$ARCH is not supported...\n"
  exit 1
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

cd $PKG
ar -x $CWD/spideroak_${VERSION}_${SRCARCH}.deb || ar -x $CWD/directdownload*$ARCH
tar xvf data.tar.gz
rm data.tar.gz control.tar.gz debian-binary
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Fix up libdir
if [ $ARCH = "x86_64" ]; then
  mv usr/lib usr/lib$LIBDIRSUFFIX
  sed -i "s|/lib/|/lib$LIBDIRSUFFIX/|g" usr/bin/SpiderOak
fi

# Kill some stuff we don't need/want
rm -rf $PKG/etc/apt

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/share/doc/$PRGNAM/* $PKG/usr/doc/$PRGNAM-$VERSION/
rm -r $PKG/usr/share/doc
cat $CWD/spideroak.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/spideroak.slackbuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -c n -l y -p $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
